//---------------------------------------------------------------------
// Author: Keith Hill, jachymko
//
// Description: Installer class copying the example profile to users
//              [MYDOCS]\WindowsPowerShell folder.
//
// Creation Date: Dec 19, 2006
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Windows.Forms;

namespace Pscx.Install
{
    [RunInstaller(true)]
    public class ProfileInstaller : PscxInstallerBase
    {
        const string ExampleProfile = "Profile\\Profile.ps1";
        const string BackupFileFormat = "{0}-{1:yyyyMMdd}#{2}.bak.ps1";
        const string PowerShellProfile = "Profile.ps1";
        const string PowerShellUserDirectory = "WindowsPowerShell";

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);

            if (ShouldInstallExampleProfile && InstallDir != null)
            {
                InstallExampleProfile();
            }
        }

        bool ShouldInstallExampleProfile
        {
            get { return Context.IsParameterTrue("InstallPscxProfile"); }
        }

        void InstallExampleProfile() 
        {
            string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string profileDir = Path.Combine(documents, PowerShellUserDirectory);

            if (!Directory.Exists(profileDir))
            {
                Directory.CreateDirectory(profileDir);
            }

            string profileDestPath = Path.Combine(profileDir, PowerShellProfile);

            if (File.Exists(profileDestPath))
            {
                string msg = String.Format(Resources.Messages.OverwriteExistingProfile, profileDestPath);

                DialogResult result = MessageBox.Show(
                                            msg,
                                            Resources.Shared.SnapinName,
                                            MessageBoxButtons.YesNo,
                                            MessageBoxIcon.Exclamation,
                                            MessageBoxDefaultButton.Button2);

                if (result == DialogResult.No)
                {
                    return;
                }

                int backupIndex = 0;
                string backupPath = string.Empty;

                do
                {
                    backupPath = string.Format(BackupFileFormat, profileDestPath, DateTime.Now, backupIndex);
                    backupIndex++;
                } 
                while (File.Exists(backupPath));

                File.Copy(profileDestPath, backupPath);
            }

            string profileSrcPath = Path.Combine(InstallDir, ExampleProfile);
            File.Copy(profileSrcPath, profileDestPath, true);
        }
    }
}
